var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin')

var env = process.env.NODE_ENV
var libName = 'ASPlayer'

function resolve(dir) {
  return path.join(__dirname, dir)
}

var webpackConfig = {
  entry: './src/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: `${libName}.js`,
    publicPath: '/',
    library: libName,
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  resolve: {
    extensions: ['.js', '.json'],
    alias: {
      'src': resolve('src'),
      'assets': resolve('src/assets')
    }
  },
  module: {
    rules: [{
      test: /\.js$/,
      loader: 'babel-loader',
      include: [resolve('src')],
      options: {
        presets: ['es2015', 'stage-0'],
        plugins: ['transform-runtime']
      }
    }, {
      test: /\.scss$/,
      use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'],
      include: [resolve('src')]
    }]
  },
  plugins: [new HtmlWebpackPlugin({
    filename: 'index.html',
    template: 'index.html',
    title: libName,
    inject: false
  })],
  devtool: 'source-map',
  devServer: {
    publicPath: '/'
  }
}

if (env !== 'dev') {
  webpackConfig.plugins.push(
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true
    })
  )
}

module.exports = webpackConfig